
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author dttung
 */
@WebServlet("/ShowTable")
public class ShowTable extends HttpServlet{
    public ShowTable()
    {
        
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, FileNotFoundException
    {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        ServletUtil.printFistHalf(out);
        Utils myUtil = new Utils();
        try {
            myUtil.readYffScores();
            myUtil.readTeamScores();
            myUtil.readTeamLinks();
            
        } catch (URISyntaxException ex) {
            Logger.getLogger(ShowTable.class.getName()).log(Level.SEVERE, null, ex);
        }
        ScoreComparator comp = new ScoreComparator(myUtil.teamsscores);
        TreeMap sortedScores = new TreeMap(comp);
        sortedScores.putAll(myUtil.teamsscores);
        out.println("<br/>\n");
        out.println("<strong>BẢNG XẾP HẠNG TATHY YFF LEAGUE\n");
        out.println("<br/>\n");
        out.println("<br/>\n");
        out.println("<table border=\"1\" bordercolor=\"#0000CC\" style=\"background-color:#FFFFCC\" width=\"100%\" cellpadding=\"5\" cellspacing=\"1\">\n" +
                    "	<tr>\n" +
                    "		<th align=\"left\">#</td>\n" +
                    "		<th align=\"left\">Team</td>\n" +
                    "		<th align=\"left\">Win</td>\n" +
                    "		<th align=\"left\">Draw</td>\n" +
                    "		<th align=\"left\">Loose</td>\n" +
                    "		<th align=\"left\">Points</td>\n" +
                    "		<th align=\"left\">Diff</td>\n" +
                    "		<th align=\"left\">Total YFF</td>\n" +
                    "	</tr>\n");
        Iterator it = sortedScores.entrySet().iterator();
        int i = 0;
        while (it.hasNext())
        {
            i++;
            Map.Entry pairs = (Map.Entry)it.next();
            String team = (String) pairs.getKey();
            
            String score= (String) pairs.getValue();
            int winpos = score.indexOf("|");        
            int win = Integer.valueOf(score.substring(0, winpos));
            int drawpos = score.indexOf("|", winpos + 1);
            int draw = Integer.valueOf(score.substring(winpos + 1, drawpos));
            int loosepos = score.indexOf("|", drawpos + 1);
            int loose = Integer.valueOf(score.substring(drawpos + 1, loosepos));
            int pointpos = score.indexOf("|", loosepos + 1);
            int point = Integer.valueOf(score.substring(loosepos + 1, pointpos)); 
            int diffpos = score.indexOf("|", pointpos + 1);
            float currdiff = Float.valueOf(score.substring(pointpos + 1, diffpos));
            float totalyff = Float.valueOf(score.substring(diffpos + 1, score.length()));
            out.println("	<tr>\n" +
                            "		<td><strong>" + i + "</strong></td>\n" +
                            "		<td><strong>" + myUtil.getTeamLinkHtml(team,"blue") + "</strong></td>\n" +
                            "		<td>" + win + "</td>\n" +
                            "		<td>" + draw + "</td>\n" +
                            "		<td>" + loose + "</td>\n" +
                            "		<td>" + point + "</td>\n" +
                            "		<td>" + currdiff + "</td>\n" +
                            "		<td>" + totalyff + "</td>\n" +
                            "	</tr>\n");
        }
        out.println("</table>\n");
        ServletUtil.printSecondHalf(out);
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
}
